home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / amok_lha / amok13.lha / Rows / Rows.mod < prev    next >
Text File  |  1993-08-15  |  5KB  |  182 lines

  1. (**********************************************************************
  2.  
  3.     :Program.    Rows.mod
  4.     :Contents.   generic data type: variable length arrays
  5.     :Author.     Nicolas Benezan [bne]
  6.     :Address.    Postwiesenstr. 2, D7000 Stuttgart 60
  7.     :Phone.      711/333679
  8.     :Copyright.  Public Domain
  9.     :Language.   Modula-2
  10.     :Translator. M2Amiga AMSoft V3.2d
  11.     :Imports.    TaskMemory [bne]
  12.     :History.    V1.0f [bne] 27.Jan.1989
  13.     :Update.     allocation procedures added [bne] 19.Jan.1989
  14.     :Bugs.       this version cannot handle Rows of Rows correctly
  15.  
  16. **********************************************************************)
  17.  
  18. IMPLEMENTATION MODULE Rows;
  19.  
  20. FROM SYSTEM     IMPORT ADDRESS,BYTE,ADR;
  21. FROM TaskMemory IMPORT Allocate,Deallocate;
  22. FROM Arts       IMPORT Assert;
  23. FROM Exec       IMPORT CopyMem;
  24.  
  25. TYPE    Row=POINTER TO Header;
  26.         Header=RECORD
  27.           MaxIndex:LONGINT;
  28.           ByteSize:CARDINAL;
  29.           AlignedSize:LONGINT;
  30.           Buffer:ADDRESS;
  31.         END;
  32. CONST   Offset=LONGINT(SIZE(Header));
  33.         Undefined="Rows: undefined Row";
  34.         IllegalSize="Rows: Size of data too big";
  35.         IllegalIndex="Rows: Index range violation";
  36.         ImportCorrupt="Rows: Import()ed corrupt Row";
  37.  
  38. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  39. (* Word alignement                                                      *)
  40. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  41. PROCEDURE Align(Bytes:LONGINT):LONGINT;
  42. BEGIN
  43.   IF ODD(Bytes) THEN
  44.     RETURN Bytes+1;
  45.   ELSE
  46.     RETURN Bytes;
  47.   END;
  48. END Align;
  49.  
  50. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  51. (* aligns size to word boundaries if neccessary                         *)
  52. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  53. PROCEDURE ActualSize(ByteSize:CARDINAL):LONGINT;
  54. BEGIN
  55.   IF ByteSize>2 THEN
  56.     RETURN Align(ByteSize);
  57.   ELSE
  58.     RETURN LONGINT(ByteSize);
  59.   END;
  60. END ActualSize;
  61.  
  62. PROCEDURE Dim(VAR row:Row;NumElements:LONGINT;
  63.               SizeOfElements:CARDINAL):BOOLEAN;
  64. VAR     Size:LONGINT;
  65. BEGIN
  66.   Size:=ActualSize(SizeOfElements);
  67.   RowsAllocProc(row,Align(NumElements*Size)+Align(Offset));
  68.   IF row#NIL THEN
  69.     WITH row^ DO
  70.       MaxIndex:=NumElements-1;
  71.       ByteSize:=SizeOfElements;
  72.       AlignedSize:=Size;
  73.       Buffer:=Align(LONGINT(row)+Offset);
  74.     END;
  75.     RETURN TRUE;
  76.   ELSE
  77.     RETURN FALSE;
  78.   END;
  79. END Dim;
  80.  
  81. PROCEDURE Discard(VAR row:Row);
  82. BEGIN
  83.   IF row#NIL THEN
  84.     RowsDeallocProc(row);
  85.   END;
  86. END Discard;
  87.  
  88. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  89. (* Checks wether index is valid for read or write operation             *)
  90. (*­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­*)
  91. PROCEDURE TestIndex(Index,MaxIndex:LONGINT);
  92. BEGIN
  93.   Assert((Index>=0)AND(Index<=MaxIndex),ADR(IllegalIndex));
  94. END TestIndex;
  95.  
  96. PROCEDURE Read(row:Row;Index:LONGINT;VAR Data:ARRAY OF BYTE);
  97. BEGIN
  98.   Assert(row#NIL,ADR(Undefined));
  99.   WITH row^ DO
  100.     TestIndex(Index,MaxIndex);
  101.     Assert(CARDINAL(HIGH(Data))<ByteSize,ADR(IllegalSize));
  102.     CopyMem(LONGINT(Buffer)+Index*AlignedSize,ADR(Data),HIGH(Data)+1);
  103.   END;
  104. END Read;
  105.  
  106. PROCEDURE Write(row:Row;Index:LONGINT;Data:ARRAY OF BYTE);
  107. BEGIN
  108.   Assert(row#NIL,ADR(Undefined));
  109.   WITH row^ DO
  110.     TestIndex(Index,MaxIndex);
  111.     Assert(CARDINAL(HIGH(Data))<ByteSize,ADR(IllegalSize));
  112.     CopyMem(ADR(Data),LONGINT(Buffer)+Index*AlignedSize,HIGH(Data)+1);
  113.   END;
  114. END Write;
  115.  
  116. PROCEDURE High(row:Row):LONGINT;
  117. BEGIN
  118.   RETURN row^.MaxIndex;
  119. END High;
  120.  
  121. PROCEDURE CompSize(row:Row):CARDINAL;
  122. BEGIN
  123.   RETURN row^.ByteSize;
  124. END CompSize;
  125.  
  126. PROCEDURE Export(row:Row;VAR Base:ADDRESS;VAR Size:LONGINT);
  127. BEGIN
  128.   IF row#NIL THEN
  129.     WITH row^ DO
  130.       Base:=Buffer;
  131.       Size:=AlignedSize*(MaxIndex+1);
  132.     END;
  133.   ELSE
  134.     Size:=0;
  135.   END;
  136. END Export;
  137.  
  138. PROCEDURE Import(VAR row:Row;NumElements:LONGINT;
  139.               SizeOfElements:CARDINAL;Base:ADDRESS;Size:LONGINT);
  140. BEGIN
  141.   Assert(row#NIL,ADR(Undefined));
  142.   WITH row^ DO
  143.     Assert((NumElements=0)OR(ByteSize=0),ADR(IllegalSize));
  144.     WITH row^ DO
  145.       MaxIndex:=NumElements-1;
  146.       ByteSize:=SizeOfElements;
  147.       AlignedSize:=ActualSize(SizeOfElements);
  148.       IF AlignedSize#LONGINT(ByteSize) THEN                   (* if size alignment was necessary *)
  149.         Assert(Base=Align(Base),ADR(ImportCorrupt)); (* base address alignment is, too *)
  150.       END;
  151.       Buffer:=Base;
  152.       Assert(Size=AlignedSize*LONGINT(NumElements),ADR(ImportCorrupt)); (* Check buffer size *)
  153.     END;
  154.   END;
  155. END Import;
  156.  
  157. PROCEDURE Assign(Source:Row;VAR Destination:Row):BOOLEAN;
  158. VAR     Base:ADDRESS;
  159.         Size:LONGINT;
  160. BEGIN
  161.   Assert(Source#NIL,ADR(Undefined));
  162.   Export(Source,Base,Size);
  163.   IF Destination#NIL THEN
  164.     Assert((Source^.ByteSize=Destination^.ByteSize)
  165.            AND(Source^.MaxIndex=Destination^.MaxIndex),ADR(IllegalSize));
  166.   ELSE
  167.     RowsAllocProc(Destination,Size);
  168.   END;
  169.   IF Destination#NIL THEN
  170.     CopyMem(Source,Destination,Size);
  171.     RETURN TRUE;
  172.   ELSE
  173.     RETURN FALSE;
  174.   END;
  175. END Assign;
  176.  
  177. BEGIN
  178.   RowsAllocProc:=Allocate;
  179.   RowsDeallocProc:=Deallocate;
  180. END Rows.
  181.  
  182.